clear all
set printcolor gs1, permanently
set more off
global dirout ""
global dirin = ""
cd "$dirin"

import excel "PANAMA_all.xlsx", sheet("Sheet1") firstrow allstring clear
destring *, replace
drop N O

rename B address_id
rename C address
capture rename *, lower
drop if missing=="x"
drop missing
rename priceruber priceuber
replace etauber="3 min" if etauber=="3 in" | etauber=="3 mn"
replace etauber="7.5" if etauber=="7--8"
replace etauber="5.5" if etauber=="5--6"
replace etauber="2 min" if etauber=="2 2 min" | etauber=="2 mini"
replace etauber="6 min" if etauber=="6 mihn" | etauber=="6 mn" | etauber=="6 mi"

replace etacabify="63" if etacabify=="1 hr 3"
replace etacabify="2 min" if etacabify=="2 mii" | etacabify=="2 m in" 
replace etacabify="9 min" if etacabify=="9 mn"
replace etacabify="4 min" if etacabify=="4 mn"
replace etacabify="4.5 min" if etacabify=="4--5"
replace etacabify="5 min" if etacabify=="5 in"
replace etacabify="5.5 min" if etacabify=="5--6"
replace etacabify="6.5 min" if etacabify=="6--7"
replace etacabify="15 min" if etacabify=="15 ,om"

replace etauber = subinstr(etauber, "away", "",.)
replace etauber = subinstr(etauber, "min", "",.)
replace etacabify = subinstr(etacabify, "min", "",.)
replace etauber = subinstr(etauber, "x", "",.)
replace etauber = subinstr(etauber, "c", "",.)
replace etacabify = subinstr(etacabify, "x", "",.)
replace etacabify = subinstr(etacabify, "s", "",.)
replace etacabify = subinstr(etacabify, "`", "1",.) 
destring etauber etacabify, replace


capture drop temp*
tab timetolocationpublic
replace timetolocationpublic = subinstr(timetolocationpublic, " ", "", .)
gen temp_hours=1 if regexm(timetolocationpublic,"hr")==1
replace temp_hours=1 if regexm(timetolocationpublic,"h")==1
gen temp_hours2=substr(timetolocationpublic,1,1)
destring temp_hours2, replace force
replace temp_hours2=. if temp_hours!=1
replace temp_hours2=temp_hours2*60  

capture drop temp_minutes
gen temp_minutes=substr(timetolocationpublic,strpos(timetolocationpublic, "hr")+2,.)
destring temp_minutes, replace force
gen temp_new_time=temp_hours2+temp_minutes

replace timetolocationpublic="" if regexm(timetolocationpublic,"hr")==1
replace timetolocationpublic="" if regexm(timetolocationpublic,"h")==1
replace timetolocationpublic = subinstr(timetolocationpublic, "min", "",.)
replace timetolocationpublic = subinstr(timetolocationpublic, "mn", "",.)
replace timetolocationpublic = subinstr(timetolocationpublic, "mi", "",.)
replace timetolocationpublic = subinstr(timetolocationpublic, "x", "",.)
destring timetolocationpublic, replace 
replace timetolocationpublic=temp_new_time if temp_new_time!=.
capture drop temp*


replace timetolocationtaxi = subinstr(timetolocationtaxi, "min", "",.)
replace timetolocationtaxi = subinstr(timetolocationtaxi, "mn", "",.)
replace timetolocationtaxi = subinstr(timetolocationtaxi, "in", "",.)
replace timetolocationtaxi = subinstr(timetolocationtaxi, ",o", "",.)
replace timetolocationtaxi = subinstr(timetolocationtaxi, "x", "",.)
destring timetolocationtaxi, replace

replace pricecabify = subinstr(pricecabify, "-Mar", "",.)
replace pricecabify = subinstr(pricecabify, "min", "",.)
replace pricecabify = subinstr(pricecabify, "x", "",.)
replace pricecabify = subinstr(pricecabify, "--", "-",.)
replace pricecabify = subinstr(pricecabify, "--", "-",.)

split pricecabify, p("-")

destring pricecabify1 pricecabify2, replace
egen pricecabify_mean=rowmean(pricecabify1 pricecabify2)
drop pricecabify1 pricecabify2

replace priceuber = subinstr(priceuber, "--", "-",.)
replace priceuber = subinstr(priceuber, "--", "-",.)
replace priceuber = subinstr(priceuber, "Feb", "",.)
replace priceuber = subinstr(priceuber, "x", "",.)
replace priceuber = subinstr(priceuber, "min", "",.)
replace priceuber = subinstr(priceuber, "`0", "10",.)

split priceuber, p("-")
destring priceuber1 priceuber2, replace
egen priceuber_mean=rowmean(priceuber1 priceuber2)
drop priceuber1 priceuber2


gen date1 = string(date,"%06.0f")
gen year=substr(date1,-2,.)
gen day=substr(date1,-4,2)
gen month=substr(date1,-6,2)
destring year day month, replace
replace year=year+2000

gen all_dates = mdy(month, day, year)
format all_dates %d 
drop date* year day month

replace timeofcollection = subinstr(timeofcollection, "x", "",.)
split timeofcollection, p(":")
replace timeofcollection1="13" if timeofcollection1=="1"
destring timeofcollection1 timeofcollection2, replace
rename timeofcollection1 hourcollection
rename timeofcollection2 minutescollection
egen collected_id=group(collected)
drop id
order  all_dates address_id

replace collected = subinstr(collected, " ", "",.)
replace collected = subinstr(collected, "Gabby", "Gaby",.)
egen id_collector=group(collected)

foreach var in etauber etacabify priceuber_mean pricecabify_mean timetolocationtaxi timetolocationpublic {
replace `var'=ln(`var')
}


* baseline
reghdfe etauber, absorb(etauber_fe=all_dates   address_id )
reghdfe etacabify, absorb(etacabify_fe=all_dates   address_id)
reghdfe priceuber_mean, absorb(priceuber_fe=all_dates   address_id)
reghdfe pricecabify_mean, absorb(pricecabify_fe=all_dates   address_id)
reghdfe timetolocationtaxi, absorb(taxi_fe=all_dates   address_id)
reghdfe timetolocationpublic, absorb(public_fe=all_dates   address_id)

keep *_fe all_dates
duplicates drop
collapse (mean) etauber_fe  priceuber_fe pricecabify_fe  etacabify_fe taxi_fe public_fe, by(all_dates)

capture drop if all_dates==22300
tsset all_dates
foreach var in etauber_fe  priceuber_fe pricecabify_fe  etacabify_fe { 
generate `var'_ma = (F1.`var' + `var' + L1.`var') / 3
}


		#delimit
		twoway
		(line priceuber_fe all_dates if all_dates<=21860, lwidth(thick) lcolor(black)) 
		(line pricecabify_fe all_dates if all_dates<=21860, lwidth(thick) lcolor(gs10)) 
		, ytitle("Log Price") title(" ") ylabel(-.3[.3].9)
		xtitle("") legend(order(1 "Uber" 2 "Cabify") region(lwidth(none)))
		xline(21822, lwidth(thick) lcolor(black) lpattern(dash))
		 text(0.6  21828  "{bf:Ban on cash}")
		xline(21846, lwidth(thick) lcolor(gs8) lpattern(dash))
		 text(0.6  21854  "{bf:Student protests}", color(gs8))	
		tlabel(15sep2019  30sep2019  15oct2019  30oct2019, format(%d)) 
		graphregion(color(white)) plotregion(fcolor(white));
		#delimit cr	
graph export "$dirout/Figure11a.eps", replace	


	#delimit
		twoway
		(line priceuber_fe all_dates if all_dates>=21921, lwidth(thick) lcolor(black)) 
		(line pricecabify_fe all_dates if all_dates>21921, lwidth(thick) lcolor(gs10)) 
		, ytitle("Log Price") title(" ") ylabel(-.4[.2].8)
		xtitle("") legend(order(1 "Uber" 2 "Cabify") region(lwidth(none)))
		xline(21951, lwidth(thick) lcolor(black) lpattern(dash))
		 text(0.6  21957  "{bf:Entry of cash}")
		xline(21934, lwidth(thick) lcolor(gs8) lpattern(dash))
		 text(0.6  21940  "{bf:Uber drivers}", color(gs8))	
		 text(0.53  21940  "{bf:protests}", color(gs8))		 
		tlabel(10jan2020  25jan2020  10feb2020  25feb2020, format(%d)) 		
		graphregion(color(white)) plotregion(fcolor(white));
		#delimit cr	
		
graph export "$dirout/Figure11b.eps", replace	

		#delimit
		twoway
		(line etauber_fe all_dates if all_dates<=21860, lwidth(thick) lcolor(black)) 
		(line etacabify_fe all_dates if all_dates<=21860, lwidth(thick) lcolor(gs10)) 
		, ytitle("Log Estimated Time of Arrival") title(" ") ylabel(-.6[.3]1.2)
		xtitle("") legend(order(1 "ETA Uber" 2 "ETA Cabify") region(lwidth(none)))
		xline(21822, lwidth(thick) lcolor(black) lpattern(dash))
		 text(0.9  21828  "{bf:Ban on cash}")
		xline(21846, lwidth(thick) lcolor(gs8) lpattern(dash))
		 text(0.9  21854  "{bf:Student protests}", color(gs8))	
		tlabel(15sep2019  30sep2019  15oct2019  30oct2019, format(%d)) 
		graphregion(color(white)) plotregion(fcolor(white));
		#delimit cr	
graph export "$dirout/Figure11c.eps", replace	


		#delimit
		twoway
		(line etauber_fe all_dates if all_dates>21860, lwidth(thick) lcolor(black)) 
		(line etacabify_fe all_dates if all_dates>21860, lwidth(thick) lcolor(gs10)) 
		, ytitle("Log Estimated Time of Arrival") title(" ") ylabel(-.6[.3]1.2)
		xtitle("") legend(order(1 "ETA Uber" 2 "ETA Cabify") region(lwidth(none)))
		xline(21951, lwidth(thick) lcolor(black) lpattern(dash))
		 text(0.9  21957  "{bf:Entry of cash}")
		xline(21934, lwidth(thick) lcolor(gs8) lpattern(dash))
		 text(0.9  21940  "{bf:Uber drivers}", color(gs8))	
		 text(0.83  21940  "{bf:protests}", color(gs8))		
		tlabel(10jan2020  25jan2020  10feb2020  25feb2020, format(%d)) 		
		graphregion(color(white)) plotregion(fcolor(white));
		#delimit cr	
graph export "$dirout/Figure11d.eps", replace	
		
		#delimit
		twoway
		(line taxi_fe all_dates if all_dates<=21860 & all_dates>21803, lwidth(thick) lcolor(black)) 
		(line public_fe all_dates if all_dates<=21860 & all_dates>21803, lwidth(thick) lcolor(gs10)) 
		, ytitle("Log Time to Location") title(" ") ylabel(-.3[.3].6)
		xtitle("") legend(order(1 "Uber & Cabify" 2 "Pubic Transport") region(lwidth(none)))
		xline(21822, lwidth(thick) lcolor(black) lpattern(dash))
		 text(0.5  21828  "{bf:Ban on cash}")
		xline(21846, lwidth(thick) lcolor(gs8) lpattern(dash))
		 text(0.5  21854  "{bf:Student protests}", color(gs8))	
		tlabel(15sep2019  30sep2019  15oct2019  30oct2019, format(%d)) 
		graphregion(color(white)) plotregion(fcolor(white));
		#delimit cr	
graph export "$dirout/Figure11e.eps", replace	


		#delimit
		twoway
		(line taxi_fe all_dates if all_dates>21860, lwidth(thick) lcolor(black)) 
		(line public_fe all_dates if all_dates>21860, lwidth(thick) lcolor(gs10)) 
		, ytitle("Log Time to Location") title(" ") ylabel(-.3[.3].6)
		xtitle("") legend(order(1 "Uber & Cabify" 2 "Pubic Transport") region(lwidth(none)))
		xline(21951, lwidth(thick) lcolor(black) lpattern(dash))
		 text(0.5  21957  "{bf:Entry of cash}")
		xline(21934, lwidth(thick) lcolor(gs8) lpattern(dash))
		 text(0.5  21940  "{bf:Uber drivers}", color(gs8))	
		 text(0.43  21940  "{bf:protests}", color(gs8))	
		tlabel(15jan2020  30jan2020  15feb2020  01mar2020, format(%d)) 		
		graphregion(color(white)) plotregion(fcolor(white));
		#delimit cr	
graph export "$dirout/Figure11f.eps", replace	
	
		

 
